package com.wanyangproject.androidlearning.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {

    /*
    * 注解annotation
    * 元注解 Java jdk自带注解
    * @Target 作用域（作用对象或范围 类，方法，接口，变量）
    * @Retention 生命周期（SOURCE：源文件    CLASS：编译期   RUNTIME：运行期）
    * @Documented 注解信息
    * @Inherited 使得被它修饰的类具有继承性
    *
    *
    * 示例：当前需要对数据库进行操作，为了将数据库中的字段进行方便的操作，我们构建了一个JavaBean类与数据表中的字段进行一一对应。
    *
    * */


    public static void main(String[] args){

        User user = new User();
        user.setAge(10);
        user.setMan(true);
        user.setName("小明");

        String quel = null;
        try {
            quel = que1(user);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println(quel);

    }

    private static String que1(User user) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        StringBuilder builder = new StringBuilder();
        builder.append("select * from ");
        Table annotation = user.getClass().getAnnotation(Table.class);
        builder.append(annotation.value());
        builder.append(" where ");
        Field[] fields = user.getClass().getDeclaredFields();
        for (Field field : fields){
            Column column = field.getAnnotation(Column.class);
            if (column == null || column.value() == null){
                continue;
            }
            if (column != null && column.value() != null){
                String methodName = field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
                Method method = user.getClass().getMethod("get"+methodName,null);
                Object methodValue = method.invoke(user,null);
                if (methodValue instanceof String){
                    if (methodValue != null){
                        builder.append(column.value());
                        builder.append(" = ");
                        builder.append(methodValue);
                        builder.append(", ");
                    }
                }else if (methodValue instanceof Integer){
                    if (((Integer) methodValue).intValue() > 0){
                        builder.append(column.value());
                        builder.append(" = ");
                        builder.append(methodValue);
                        builder.append(", ");
                    }
                }else if (methodValue instanceof Boolean){

                }


            }
        }


        return builder.toString();
    }


}
